package com.zdy.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import com.zdy.utils.DensityUtil;


/**
 * 自定义文字滤镜
 * Created by zdy on 2016/10/28.
 */
public class ZdyView extends View {

    private Paint mPaint;
    private Context context;
    private int mViewWidth;

    public ZdyView(Context context) {
        super(context);
        init(context);
    }


    public ZdyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public ZdyView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        this.context = context;
        mPaint = new Paint();
        mPaint.setColor(Color.parseColor("#398de3"));
    }

    RectF rect;
    RectF rect1;
    private int count = 2;

    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (distance_left > (mViewWidth * (count-1)*1.0 / count - DensityUtil.dip2px(context, 5)) && distance_left <= mViewWidth * (count-1)*1.0 / count) {
            rect = new RectF(distance_left, getTop(), distance_right, getBottom());
            canvas.drawRoundRect(rect, DensityUtil.dip2px(context, 5), DensityUtil.dip2px(context, 5), mPaint);
            rect1 = new RectF(distance_left, getTop(), distance_left + DensityUtil.dip2px(context, 5), getBottom());
            canvas.drawRect(rect1, mPaint);
        } else if (distance_left > DensityUtil.dip2px(context, 5)) {
            rect = new RectF(distance_left, getTop(), distance_right, getBottom());
            canvas.drawRect(rect, mPaint);
        } else {
            rect = new RectF(distance_left, getTop(), distance_right, getBottom());
            canvas.drawRoundRect(rect, DensityUtil.dip2px(context, 5), DensityUtil.dip2px(context, 5), mPaint);
            rect1 = new RectF(distance_right - DensityUtil.dip2px(context, 5), getTop(), distance_right, getBottom());
            canvas.drawRect(rect1, mPaint);
        }
    }

    private float distance_left;
    private float distance_right;

    public void moveToRight(float ds, float pos) {
        distance_left = mViewWidth * (pos - 2f) / count + ds * mViewWidth / count;
        distance_right = ds * mViewWidth / count + mViewWidth / count * (pos - 1f);
//        Log.e("zdy", "moveToRight distance_left" + distance_left);
//        Log.e("zdy", "moveToRight distance_right" + distance_right);
        postInvalidate();
    }

    public void moveToLeft(float ds, float pos) {
        distance_left = mViewWidth * (pos - 2f) / count + (1 - ds) * mViewWidth / count;
        distance_right = mViewWidth * (pos / count) - ds * mViewWidth / count;
//        Log.e("zdy", "moveToLeft distance_left" + distance_left);
//        Log.e("zdy", "moveToLeft distance_right" + distance_right);
        postInvalidate();
    }


    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        if (mViewWidth == 0) {
            mViewWidth = getMeasuredWidth();
        }
    }
}
